/********************************************************/
/*	Copyright (C) 2012 Gong Li Bin			*/
/*	Project:	MapReduce			*/
/*	Author:		gong_libin			*/
/*	Date:		2013_05_06			*/
/*	File:		MrMysql.cpp			*/
/********************************************************/

#include "MrMysql.h"

CMrMysql::CMrMysql()
{
}

CMrMysql::~CMrMysql()
{
}

char* CMrMysql::MrMysqlError()
{
	return (char*)mysql_error(&m_stMysql);
}

void CMrMysql::MrMysqlFree(void* pResult)
{
	mysql_free_result((MYSQL_RES*)pResult);

	return;
}

void CMrMysql::MrSqlClose()
{
	mysql_close(&m_stMysql);

	return;
}

int CMrMysql::MrSqlInsert(char* pszSql)
{
	return MrMysqlQuery(pszSql);
}

int CMrMysql::MrSqlUpdate(char* pszSql)
{
	return MrMysqlQuery(pszSql);
}

int CMrMysql::MrSqlDelete(char* pszSql)
{
	return MrMysqlQuery(pszSql);
}

void* CMrMysql::MrSqlSelect(char* pszSql)
{
	void* pReturn = NULL;

	if (MR_SUCCESS == MrMysqlQuery(pszSql)) {
		pReturn = (void*)mysql_store_result(&m_stMysql);
	}

	return pReturn;
}

int CMrMysql::MrSqlConnect(char* pszHost, char* pszPort, char* pszUser, char* pszPass)
{
	int iReturn = MR_SUCCESS;

	if (NULL != mysql_init(&m_stMysql) ){
		if (NULL ==  mysql_real_connect(&m_stMysql, pszHost, pszUser, pszPass, NULL, atoi(pszPort), NULL, 0)) {
			MR_ERROR("%s\n", strerror(errno));
			iReturn = MR_FAILURE;
		}
	}
	else {
		MR_ERROR("%s\n", strerror(errno));
		iReturn = MR_FAILURE;
	}

	return iReturn;
}

int CMrMysql::MrMysqlQuery(char* pszSql)
{
	if (MR_SUCCESS != mysql_query(&m_stMysql, pszSql)) {
		MR_ERROR("%s\n", MrMysqlError());
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}
